package com.easywan.web.controller.gtd;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easywan.core.BaseController;
import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.pojo.PageRow;
import com.easywan.service.IGtdService;
import com.easywan.vo.Gtd;
import com.easywan.vo.GtdStep;

import junit.framework.Assert;
/**
 * 
 * @author tangkang
 *
 */
@Controller
@RequestMapping("/users/work/gtd/step")
public class GtdStepController extends BaseController{
	@Autowired
	private IGtdService gtdService;
	private Logger logger = Logger.getLogger(GtdStepController.class);
	@RequestMapping("index")
	public String index(HttpServletRequest request,ModelMap modeMap){
		try{
			modeMap.addAttribute("href","/users/work/gtd.html");
			Integer uid = getUsersId(request);
			Integer id = getInt(request, "id");
			Gtd gtd = gtdService.getByUsersIdAndId(uid,id);
			modeMap.addAttribute("data",gtd);
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return "users/work/gtd/step/index.jsp";
	}
	@ResponseBody
	@RequestMapping("stepList")
	public String list(HttpServletRequest request,PageRow pageRow){
		IResult result = null;
		try{
			Integer gid = getInt(request, "sid");
			Integer uid = getUsersId(request);
			paramsNotNull(gid,uid);
			result = gtdService.getGtdStep(gid,uid,pageRow);
		}catch(Exception e){
			logger.error(e.getMessage());
			result = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(result);
	}
	@RequestMapping("add")
	public String add(HttpServletRequest request,ModelMap modeMap){
		try{
			Integer uid = getUsersId(request);
			Integer id = getInt(request, "gid");
			modeMap.addAttribute("href","/users/work/gtd/step/index.html?id="+id);
			Gtd gtd = gtdService.getByUsersIdAndId(uid,id);
			modeMap.addAttribute("data",gtd);
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return "users/work/gtd/step/add.jsp";
	}
	@ResponseBody
	@RequestMapping("addStep")
	public String addStep(HttpServletRequest request){
		IResult result = null;
		try{
			Integer gid = getInt(request, "gid");
			Integer uid = getUsersId(request);
			String name = getString(request,"name");
			String bief = getString(request,"bief");
			String startDate = getString(request,"startDate");
			String endDate = getString(request,"endDate");
			paramsNotNull(gid,uid);
			result = gtdService.addGtdStep(gid,uid,name,bief,startDate,endDate);
		}catch(Exception e){
			logger.error(e.getMessage());
			result = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(result);
	}
	@ResponseBody
	@RequestMapping("del")
	public String del(HttpServletRequest request){
		IResult rst = null;
		try{
			Integer id = getInt(request, "sid");
			rst = gtdService.delStepToTrash(getUsersId(request),id);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst = IResultSupport.buildResult(IResult.FAILT_CODE, e.getMessage());
		}
		return toJSON(rst);
	}
	@RequestMapping("gtdModify")
	public String gtdModity(HttpServletRequest request,ModelMap model){
		try{
			Integer id = getInt(request, "id");
			GtdStep step = gtdService.getStepByUsersIdAndId(getUsersId(request),id);
			model.addAttribute("step",step);
			}catch(Exception e){
				logger.error("查看gtd:"+getUserInfo(request,"")+e.getMessage());
				e.printStackTrace();
			}
		return toPath("users/work/gtd/step/modify.jsp");
	}
}
